package com.gowants.pems.util;

import java.security.MessageDigest;
import java.util.Random;

import com.gowants.pems.util.Const;

public class MD5
{
    private static final char[] VALID_CODES = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8'};
    /**
     * 
     * <生成token>
     * @return
     * @remake create by GJR 2019年5月10日 上午9:38:50
     */
    public static String getToken() 
    {
       
        // 创建一个随机数生成器类
        Random random = new Random();
        String strRand = String.valueOf(VALID_CODES[random.nextInt(29)]);
        StringBuffer sb = new StringBuffer();
        sb.append("token_validate_code:");
        sb.append(strRand);
        
        return md5(sb.toString());
    }
    
    public static String md5(String str)
    {
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++)
            {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            str = buf.toString();
        } catch (Exception e)
        {
            e.printStackTrace();

        }
        return str;
    }

    public static void main(String[] args)
    {
        System.out.println(md5("123456"));
        System.out.println(md5("gw123456"));
        System.out.println(md5("tsms_token_cookie"));
        System.out.println("admin[" + md5("admin") + "]");
        
        System.out.println("31321".matches(Const.NUMBER_REGIX));
        System.out.println("3132".matches(Const.LOGIN_NAME_REG));
        System.out.println("192.156.1.35".matches(Const.HOST_MANAGER_IP));
        System.out.println("('1', ".substring(0, "('1', ".length()-2));
    }
}
